/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var feedback_bar = "<img src='images/ajax_loader_bar.gif'/>";

function increment(layer){
    var curVal = parseInt(document.m1.txtQty.value);
    if(curVal >= 10) {
        alert('Out of stock');
    }
    else{
        document.m1.txtQty.value = curVal + 1;
    }
}  ///NOT USED

function decrement(layer){
    var curVal = parseInt(document.m1.txtQty.value);
    if(curVal <= 1) {
        alert('Minimum purchasing amount is 1');
    }
    else{
        document.m1.txtQty.value = curVal - 1;
    }
} ///NOT USED

var state = 0;
function addReview(){
    if(state == 0){
        state = 1;
        document.getElementById('addReview').style.display = 'block';
        document.getElementById('comment').value = "";
    }
    else{
        state = 0;
        document.getElementById('addReview').style.display = 'none';
        document.getElementById('comment').value  = "";
    }
} ///NOT USED

var xmlhttpObj;
var postContent;
function postReview(){
    var xContents = document.getElementById('comments');
    postContent = document.getElementById('respond').innerHTML;


    var comment_email = document.getElementById('email').value;
    var comment_name = document.getElementById('name').value;
    var xcomment_contents = document.getElementById('reviewText').value;

    document.getElementById('respond').innerHTML = feedback_bar;
    xmlhttpObj = createAjaxObject();
    if(this.xmlhttpObj!=null){
        this.xmlhttpObj.onreadystatechange = timedMsg;
        xmlhttpObj.open("GET", "Process.php?event=100&comment="+xcomment_contents+ "&user=" + comment_name + "&email=" + comment_email, true);
        xmlhttpObj.send(null);
    }

}

function timedMsg()
{
    if (xmlhttpObj.readyState==4 || xmlhttpObj.readyState=="complete")
    {
        var t=setTimeout("myCallBack_Review()",1000);        
    }
}

function myCallBack_Review(){
    if (xmlhttpObj.readyState==4 || xmlhttpObj.readyState=="complete")
    {
        var xContents = document.getElementById('commentlist');
        var xli = document.createElement("li");
        xli.className = "comment_odd";
        var xDiv = document.createElement("div");


        document.getElementById('respond').innerHTML = postContent;
         
        xDiv.innerHTML = xmlhttpObj.responseText;
        xli.appendChild(xDiv);
        xContents.appendChild(xli);

    }
}

function createAjaxObject(){

    if(window.ActiveXObject){
        //alert('IE 6');
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest){
        return new XMLHttpRequest();
    }
    else{
        //alert('Null');
        alert('Browser AJAX unsupported !!!');
        return  null;
    }
}

function hideDiv() {
    document.getElementById('hideshow').style.visibility = 'hidden';
}

function showDiv() {
    document.getElementById('hideshow').style.visibility = 'visible';
}

function clearDefaultText(){
    document.getElementById('txtSearch').value = "";
}

function resoreDefaultText(){
    if(document.getElementById('txtSearch').value == "Phone Model" || document.getElementById('txtSearch').value == "")
        document.getElementById('txtSearch').value = "Phone Model";
}


/*--------------------------------------------P A G I N G ----------------------------------------------*/

function firstLoad(){
    xmlhttpObj=createAjaxObject();
    document.getElementById("searchTable").innerHTML = feedback_bar;
    xmlhttpObj.onreadystatechange=timedSearch;
    xmlhttpObj.open("GET", "Process.php?event=150&direction=none", true);
    xmlhttpObj.send(null);
}

function moveFw(currPage,lastPage){
    createAjaxObject();
    document.getElementById("searchTable").innerHTML = feedback_bar;
    xmlhttpObj.onreadystatechange=timedSearch;
    xmlhttpObj.open("GET", "Process.php?event=150&direction=fw&currPage="+currPage+"&lastPage="+lastPage, true);
    xmlhttpObj.send(null);
}

function moveBk(currPage,lastPage){
    createAjaxObject();
    document.getElementById("searchTable").innerHTML = feedback_bar;
    xmlhttpObj.onreadystatechange=timedSearch;
    xmlhttpObj.open("GET", "Process.php?event=150&direction=bk&currPage="+currPage+"&lastPage="+lastPage, true);
    xmlhttpObj.send(null);
}

function myCallBack_displayPage(){
    if(xmlhttpObj.readyState==4)
    {
        //alert(xmlhttpObj.responseText);
        var myJson = eval('(' + xmlhttpObj.responseText + ')');
        var currPage, lastPage;
        var output="<table class=\"browseTable\" cellpadding=\"0\" cellspacing=\"0\">";
        for(i=0;i<myJson.data.length;i=i+2)
        {
            //alert(myJson.data.length +" i:" +i);
            var x = (i+1);
            output = output + "<tr><td><div class=\"tableDiv\"><center><img align=\"middle\" src=\"images/products/"+ myJson.data[i].url+"\" width=\"100px\" height=\"150px\" alt=\"\"/><br/>"
            + "<p>"+myJson.data[i].brand+ " " +myJson.data[i].model + "</p>"
            + "<p sytle=\"font-size: xx-small;\">"+myJson.data[i].desc+"</p>"
            + "<input class=\"btn\" type =\"button\" value=\"Add to Cart\" name=\"btnCart\" />&nbsp;"
            + "<a href=\"#\" ><input class=\"btn\" type =\"button\" value=\"Read More\"/></a>"
            + "</center> </div></td>";
            if(x<myJson.data.length){
                output = output + "<td><div class=\"tableDiv\"><center><img align=\"middle\" src=\"images/products/"+ myJson.data[x].url+"\" width=\"100px\" height=\"150px\" alt=\"\"/><br/>"
                + "<p>"+myJson.data[(x)].brand+ " " +myJson.data[(x)].model + "</p>"
                + "<p>"+myJson.data[(x)].desc+"</p>"
                + "<input class=\"btn\" type =\"button\" value=\"Add to Cart\" name=\"btnCart\" />&nbsp;"
                + "<a href=\"#\" ><input class=\"btn\" type =\"button\" value=\"Read More\"/></a></center></div></td></tr>";
            }
            currPage = myJson.data[i].cPage;
            lastPage = myJson.data[i].lastPage;
        //            alert("$lastpage"+myJson.data[i].miss);
        }

        output = output + "</table><div style=\"clear:both\"></div>"; //<input type=\"hidden\" name=\"lastRec\" value="+ lastRec +"/>
        displayNext(++currPage, ++lastPage);
        document.getElementById("searchTable").innerHTML = output ;
    }
}

function displayNext(currPage,lastPage){
    alert(currPage);
    alert(lastPage);
    var naviDivContent = "";

    if(currPage>1){
        currPage--;
        naviDivContent += "<input type =\"button\" Value=\"Previous\" onclick=\"javascript:moveBk("+currPage+","+lastPage+");\" />";
    }
    if(currPage<lastPage){
        alert(currPage);
        currPage++;
        alert(currPage);
        naviDivContent += "<input type =\"button\" Value=\"Next\" onclick=\"javascript:moveFw("+currPage+","+lastPage+");\" />";
    }
    document.getElementById('pageDiv').innerHTML = naviDivContent;
    return;
} // Show/Hide Previous - Next Buttons

function timedSearch()
{
    if (xmlhttpObj.readyState==4 || xmlhttpObj.readyState=="complete")
    {
        var t=setTimeout("myCallBack_displayPage()",1000);
    }
}

function submit(){
    alert('Fuck ');
    var brand = document.getElementById('product');
    var price = document.getElementById('price');
    var feature = document.getElementById('feature');

    var lenBrand = brand.length;
    var lenPrice = price.length;
    var lenFeature = feature.length;

    for(var i=0; i < lenBrand; i++){
        if(lenBrand.options[i].selected == true){
            lenBrand.options[i].selected = true;
        }
    }

    for(var j=0; j < lenPrice; j++){
        if(lenPrice.options[j].selected == true){
            lenPrice.options[j].selected = true;
        }
    }

    for(var k=0; k < lenFeature; k++){
        if(lenFeature.options[k].selected == true){
            lenFeature.options[k].selected = true;
        }
    }

} // This is the function that keep selected the index